package com.mystore.manage.controller;

import com.mystore.common.paging.StylePage;
import com.mystore.framework.pojo.Uri;
import com.mystore.framework.service.impl.IUriServiceImpl;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * URI表(Uri)表控制层
 */
@Controller
@RequestMapping("manage/uri")
public class UriController {
    /**
     * 服务对象
     */
    @Resource
    private IUriServiceImpl iUriServiceImpl;

    @GetMapping("/page.do")
    public String page(@RequestParam(defaultValue="") String data,Model model, @RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long size,String result) {
        IPage<Uri> uriPage = iUriServiceImpl.selectPageByData(data,new Page<>(current, size));
        model.addAttribute("uris", new StylePage<>(uriPage));
        model.addAttribute("result",result);
        model.addAttribute("data",data);
        return "uri_page";
    }

    @GetMapping("/insert.do")
    public String insert(Model model) {
        model.addAttribute("mode", "insert");
        return "uri_info";
    }

    @GetMapping("/update.do")
    public String update(Model model, int uriId) {
        model.addAttribute("uri", iUriServiceImpl.selectById(uriId));
        model.addAttribute("mode", "update");
        return "uri_info";
    }

    @PostMapping("/save.do")
    public String save(RedirectAttributes model, Uri uri, String mode) {
        if (mode.equals("insert"))
            model.addAttribute("result", iUriServiceImpl.insertOne(uri)>0);
        else
            model.addAttribute("result", iUriServiceImpl.updateOne(uri));
        model.addAttribute("mode", mode);
        return "redirect:/manage/uri/page.do";
    }

    @GetMapping("/delete.do")
    public String delete(RedirectAttributes model, int uriId) {
        model.addAttribute("result", iUriServiceImpl.deleteById(uriId));
        return "redirect:/manage/uri/page.do";
    }


}

